package xiaohu.day6.HJ68;


import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String n = scanner.nextLine();
            String sortA = scanner.nextLine();
            TreeMap<Integer, String> treeMap = new TreeMap<>();
            for (int i = 0; i < Integer.parseInt(n); i++) {
                String[] split = scanner.nextLine().split(" ");
                if (!treeMap.containsKey(Integer.parseInt(split[1]))) {
                    treeMap.put(Integer.parseInt(split[1]), split[0]);
                } else {
                    treeMap.put(Integer.parseInt(split[1]), treeMap.get(Integer.parseInt(split[1])) + " " + split[0]);
                }
            }


            if (Integer.valueOf(sortA) == 0) {
                for (Map.Entry<Integer, String> entry : treeMap.descendingMap().entrySet()) {
                    String[] split = entry.getValue().split(" ");
                    if (split.length == 1) {
                        System.out.println(entry.getValue() + " " + entry.getKey());
                    } else {
                        for (int i = 0; i < split.length; i++) {
                            System.out.println(split[i] + " " + entry.getKey());
                        }
                    }
                }
            } else {
                for (Map.Entry<Integer, String> entry : treeMap.entrySet()) {
                    String[] split = entry.getValue().split(" ");
                    if (split.length == 1) {
                        System.out.println(entry.getValue() + " " + entry.getKey());
                    } else {
                        for (int i = 0; i < split.length; i++) {
                            System.out.println(split[i] + " " + entry.getKey());
                        }
                    }
                }
            }
        }
    }
}
